package com.huanzhidadi.single.delayquit;

import lombok.Data;

import java.util.Objects;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class QuitUserKey implements Delayed {

    private String key;

    private long avaibleTime;

    public QuitUserKey(String key, long delayTime) {
        this.key = key;
        this.avaibleTime = delayTime + System.currentTimeMillis();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        // 判断avaibleTime是否大于当前系统时间
        long diffTime = avaibleTime - System.currentTimeMillis();
        return unit.convert(diffTime, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.avaibleTime - ((QuitUserKey) o).getAvaibleTime());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        QuitUserKey that = (QuitUserKey) o;
        return key.equals(that.key);
    }

    @Override
    public int hashCode() {
        return Objects.hash(key);
    }
}
